/*
 * @description: 移动端仿苹果弹出框
 * @author: 宫晓辉
 * @update: 20191230
 */	
!function(window){
	/**
	 * 消息警告框
	 * @param {提示对话框上显示的内容} message
	 * @param {提示对话框上显示的标题} title
	 * @param {提示对话框上按钮显示的内容} buttonCapture
	 */
	window.alert = function(message,title,buttonCapture) {
		//排除参数为空
		message == null ? message="null" : message,
		title == null ? title="提示信息" : title,
		buttonCapture == null ? buttonCapture="确定" : buttonCapture;
		//遮罩层
		var popupBackdrop = document.createElement("div");
		popupBackdrop.setAttribute("class","popup-backdrop fadeIn");
		popupBackdrop.setAttribute("id","popup-backdrop");
		//弹窗层
		var noticePopup = document.createElement("div");
		noticePopup.setAttribute("id","noticePopup");
		noticePopup.setAttribute("class","own-popup");
		
		var appendDom = "<div id='popup-in' class='popup-animated pulse'><div class='popup-inner line-bot'><div class='popup-title'>"+title+"</div><div class='popup-text'>"+message+"</div></div><div class='popup-buttons' id='notice' onclick=\"closeAlert(this)\"><span class='popup-button' ontouchstart=btn_touchstart(this); ontouchend=btn_ontouchend(this);>"+buttonCapture+"</span></div></div>";
		noticePopup.innerHTML = appendDom;
		
		document.body.appendChild(popupBackdrop);
		document.body.appendChild(noticePopup);
		
		document.getElementById("popup-backdrop").style.display = "block";
		document.getElementById("noticePopup").style.display = "flex";
	};


	/**
	 * 消息确认框
	 * @param {确认对话框上显示的内容} message
	 * @param {确认对话框上显示的标题} title
	 * @param {返回值} callback
	 */
	var confirmCallback = null;
	window.confirm = function(message,title,callback) {
		if(callback) confirmCallback = callback;
		//排除参数为空
		message == null ? message="null" : message,
		title == null ? title="提示信息" : title;
		//遮罩层
		var popupBackdrop = document.createElement("div");
		popupBackdrop.setAttribute("class","popup-backdrop fadeIn");
		popupBackdrop.setAttribute("id","popup-backdrop");
		//弹窗层
		var confirmPopup = document.createElement("div");
		confirmPopup.setAttribute("id","confirmPopup");
		confirmPopup.setAttribute("class","own-popup");
		
		var appendDom = '<div id="popup-in" class="popup-animated pulse"><div class="popup-inner line-bot"><div class="popup-title">'+title+'</div><div class="popup-text">'+message+'</div></div><div class="popup-buttons"><span class="popup-button" id="confirmY" onclick=closeAlert(this); ontouchstart=btn_touchstart(this); ontouchend=btn_ontouchend(this);>是</span><span class="popup-button line-left" id="confirmX" onclick=closeAlert(this); ontouchstart=btn_touchstart(this); ontouchend=btn_ontouchend(this);>否</span></div></div>';
		confirmPopup.innerHTML = appendDom;
		
		document.body.appendChild(popupBackdrop);
		document.body.appendChild(confirmPopup);
		
		document.getElementById("popup-backdrop").style.display = "block";
		document.getElementById("confirmPopup").style.display = "flex";
	};

	/**
	 * 提示框
	 * @param {输入对话框上显示的内容} message
	 * @param {输入对话框上显示的标题} title
	 * @param {输入对话框上编辑框显示的提示文字} tip
	 * @param {返回值} callback
	 */
	var promptCallback = null;
	window.prompt = function(message,title,tip,callback) {
		if(callback) promptCallback = callback;
		//排除参数为空
		message == null ? message="null" : message,
		title == null ? title="请输入信息" : title,
		tip == null ? tip="请输入内容" : tip;
		//遮罩层
		var popupBackdrop = document.createElement("div");
		popupBackdrop.setAttribute("class","popup-backdrop fadeIn");
		popupBackdrop.setAttribute("id","popup-backdrop");
		//弹窗层
		var promptPopup = document.createElement("div");
		promptPopup.setAttribute("id","dialoguePopup");
		promptPopup.setAttribute("class","own-popup");
		
		var appendDom = '<div id="popup-in" class="popup-animated pulse"><div class="popup-inner line-bot"><div class="popup-title">'+title+'</div><div class="popup-text">'+message+'</div><div class="popup-input"><input type="text" name="promptObtain" autofocus="" placeholder='+tip+'></div></div><div class="popup-buttons"><span class="popup-button" id="dialogueY" onclick=closeAlert(this); ontouchstart=btn_touchstart(this); ontouchend=btn_ontouchend(this);>确定</span><span class="popup-button line-left" id="dialogueX" onclick=closeAlert(this); ontouchstart=btn_touchstart(this); ontouchend=btn_ontouchend(this);>取消</span></div></div>';
		promptPopup.innerHTML = appendDom;
		
		document.body.appendChild(popupBackdrop);
		document.body.appendChild(promptPopup);
		
		document.getElementById("popup-backdrop").style.display = "block";
		document.getElementById("dialoguePopup").style.display = "flex";
	};

	/**
	 * toast提示
	 * @param {输入提示框上显示的内容} message
	 * @param {输入提示框隐藏的延时时间} time,默认为1.5s
	 */
	window.toast = function(message,time,type){
		type = type || "succ"; // err为错误提示，warn为警告
		//排除参数为空
		message == null ? message="null" : message,
		time == null ? time = "1500" : time;
		//遮罩层
		var popupBackdrop = document.createElement("div");
		popupBackdrop.setAttribute("class","popup-backdrop");
		popupBackdrop.setAttribute("id","popup-backdrop");
		popupBackdrop.style.background = "transparent";

		//弹窗层
		var promptPopup = document.createElement("div");
		promptPopup.setAttribute("id","toastPopup");
		promptPopup.setAttribute("class","own-popup");
		
		var appendDom = '<div id="popup-toast" class="toast-import">'+(type=="succ"?'<svg t="1622882982996" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5988" width="28" height="28"><path d="M510.567403 63.852903c-246.304387 0-446.663336 200.358949-446.663336 446.663336 0 246.304387 200.358949 446.663336 446.663336 446.663336 246.304387 0 446.765664-200.358949 446.765664-446.663336C957.230738 264.211852 756.87179 63.852903 510.567403 63.852903L510.567403 63.852903zM787.979614 386.084941 454.593784 719.573099c-7.981613 7.981613-20.977316 7.981613-28.958929 0l-43.694214-43.694214c0 0 0 0 0 0L237.145998 531.084241c-7.981613-7.981613-7.981613-20.977316 0-28.958929l43.694214-43.694214c7.981613-7.981613 20.977316-7.981613 28.958929 0L440.063156 588.592785 715.326471 313.329469c7.981613-7.981613 20.977316-7.981613 29.061257 0L787.979614 357.126012C796.063556 365.107625 796.063556 378.103328 787.979614 386.084941L787.979614 386.084941z" p-id="5989" data-spm-anchor-id="a313x.7781069.0.i30" class="selected" fill="#ffffff"></path></svg>':type=="err"?'<svg t="1622883398958" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="19692" width="28" height="28"><path d="M511.4 63.6c-247.4 0-448 200.6-448 448s200.6 448 448 448 448-200.6 448-448c-0.1-247.5-200.6-448-448-448z m196.2 583.8c16.7 16.7 16.7 43.7 0 60.4-16.7 16.7-43.7 16.6-60.4-0.1L511.4 571.9 375.5 707.8c-16.7 16.6-43.7 16.6-60.4 0-16.7-16.7-16.7-43.7 0-60.4L451 511.5 315.1 375.7c-16.7-16.7-16.7-43.7 0-60.4 16.7-16.6 43.7-16.6 60.4 0l135.9 135.9 135.9-135.9c16.6-16.6 43.7-16.6 60.4 0 16.7 16.7 16.7 43.7 0 60.4L571.8 511.6l135.8 135.8z" p-id="19693" data-spm-anchor-id="a313x.7781069.0.i60" class="selected" fill="#ffffff"></path></svg>':'<svg t="1622883574779" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="28196" width="24" height="24"><path d="M512 0C229.254842 0 0.010628 229.244214 0.010628 511.989372c0 282.766414 229.244214 512.010628 511.989372 512.010628 282.766414 0 511.989372-229.244214 511.989372-512.010628C1024.010628 229.244214 794.78767 0 512 0zM580.146217 804.23589l-136.271178 0L443.875039 687.626362l136.271178 0L580.146217 804.23589zM580.146217 591.443695l-136.271178 0L443.875039 219.76411l136.271178 0L580.146217 591.443695z" p-id="28197" data-spm-anchor-id="a313x.7781069.0.i66" class="selected" fill="#ffffff"></path></svg>')+
'<span>'+message+'</span></div>';

		promptPopup.innerHTML = appendDom;
		
		document.body.appendChild(popupBackdrop);
		document.body.appendChild(promptPopup);

		var timer = setTimeout(function(){
			document.querySelector(".toast-import").setAttribute("class","toast-import fadeOut");
			var timer2 = setTimeout(function(){
				closeFn();
				clearTimeout(timer2);
			},500);
			clearTimeout(timer);
		},time);
	};
	
	/**
	 * loading
	 * @param {输入loading框上显示的内容} message
	 */
	window.showLoading = function(message){
		//排除参数为空
		message == null ? message="加载中..." : message;
		//遮罩层
		var popupBackdrop = document.createElement("div");
		popupBackdrop.setAttribute("class","popup-backdrop fadeIn");
		popupBackdrop.setAttribute("id","popup-backdrop");
		popupBackdrop.style.background = "transparent";
	
		//弹窗层
		var promptPopup = document.createElement("div");
		promptPopup.setAttribute("id","loadingPopup");
		promptPopup.setAttribute("class","own-popup");
		
		var appendDom = '<div id="popup-toast" style="padding:14px 20px;"><img style="margin-bottom:10px" src=""><br>'+message+'</div>';
		promptPopup.innerHTML = appendDom;
		
		document.body.appendChild(popupBackdrop);
		document.body.appendChild(promptPopup);
	};
	//关闭loading
	window.hideLoading = function(){
		closeFn();
	};

	/**
	 * 判断是pc还是移动端
	 */
	function isPC(){
		let Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");  
		for (var v = 0; v < Agents.length; v++) {  
			if (navigator.userAgent.indexOf(Agents[v])>=0) { 
				return false;
				break; 
			}else{
				return true;
			}
		};
	};

	isPC = isPC();

	/**
	 * 关闭 消息框
	 */
	function closeFn(_this){
		if(confirmCallback){
			confirmCallback(_this.id=="confirmY"?true:false);
			confirmCallback = null;
		};

		if(promptCallback){
			if(_this.id=="dialogueY"){
				promptCallback(document.getElementsByName("promptObtain")[0].value);
			};
			promptCallback = null;
		};

		document.body.removeChild(document.getElementById("popup-backdrop"));
		document.body.removeChild(document.querySelector(".own-popup"));
	};

	window.closeAlert = function(_this) {
		if(!isPC) return;
		
		closeFn(_this);
	};

	window.btn_touchstart = function(_this) {
		_this.style.backgroundColor = "rgba(234,234,235,0.95)";
	};

	window. btn_ontouchend = function(_this) {
		_this.style.backgroundColor = "rgba(255,255,255,.95)";
		closeFn(_this);
	};

	//插入样式
	var _style = document.createElement("style");
	_style.innerHTML = '#popup-in,#popup-toast,.own-popup,.popup-backdrop,.popup-button,.popup-buttons,.popup-inner,.popup-input,.popup-input input,.popup-text,.popup-title{box-sizing:border-box;-webkit-user-select:none;-webkit-tap-highlight-color:transparent;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}.line-bot::after{content:"";width:100%;height:1px;background:#bbb;position:absolute;left:0;bottom:0;transform:scaleY(.5)}.line-top::after{content:"";width:100%;height:1px;background:#bbb;position:absolute;left:0;top:0;transform:scaleY(.5)}.line-left::before{content:"";height:100%;width:1px;background:#bbb;position:absolute;left:0;top:0;transform:scaleX(.5)}.own-popup{border:0 red solid;position:fixed;z-index:99999;top:0;left:0;right:0;bottom:0;display:flex;justify-content:center;align-items:center}#popup-in{width:270px;border-radius:13px;overflow:hidden;text-align:center;-webkit-animation-name:contentZoomIn;animation-name:contentZoomIn}.popup-inner{position:relative;padding:15px;border-radius:13px 13px 0 0;background:rgba(255,255,255,.95)}.popup-title{font-size:18px;font-weight:500;text-align:center}.popup-title+.popup-text{font-family:inherit;font-size:14px;margin:10px 0 0;text-align:center;color:#000}.popup-buttons{border:0 #00f solid;position:relative;display:-webkit-box;display:-webkit-flex;display:flex;height:44px;-webkit-box-pack:center;-webkit-justify-content:center;justify-content:center}.popup-button{border:0;font-size:17px;line-height:44px;position:relative;display:block;overflow:hidden;box-sizing:border-box;width:100%;height:44px;padding:0 5px;cursor:pointer;text-align:center;white-space:nowrap;text-overflow:ellipsis;color:#007aff;background:rgba(255,255,255,.95);-webkit-box-flex:1}.popup-button:last-child{border-radius:0 0 13px}.popup-button:first-child{border-radius:0 0 0 13px}.popup-animated{animation-duration:.3s;animation-fill-mode:both}.popup-input input{font-size:14px;width:100%;height:26px;margin:15px 0 0;padding:0 5px;border:1px solid rgba(0,0,0,.3);border-radius:0;background:#fff;outline:0;-webkit-user-select:auto}#popup-toast{line-height:1;text-align:center;position:fixed;max-width:200px;z-index:9999;padding:14px;color:#fff;background:#000;border-radius:5px;left:50%;top:50%;font-size:14px;-webkit-transform:translate(-50%,-50%);transform:translate(-50%,-50%)}.toast-import{background:rgba(0,0,0,.8)!important;display:flex;align-items:center;animation-duration:.5s;animation-fill-mode:both;-webkit-animation-name:bgFadeIn;animation-name:bgFadeIn}.toast-import.fadeOut{-webkit-animation-name:bgFadeOut;animation-name:bgFadeOut}.toast-import>span{margin-left:8px}.popup-backdrop{position:fixed;z-index:99998;top:0;right:0;bottom:0;left:0;opacity:1;background:rgba(0,0,0,.4);-webkit-animation-duration:.5s;animation-duration:.5s}.popup-backdrop.fadeIn{-webkit-animation-name:bgFadeIn;animation-name:bgFadeIn}@-webkit-keyframes bgFadeIn{0%{opacity:0}100%{opacity:1}}@keyframes bgFadeIn{0%{opacity:0}100%{opacity:1}}@-webkit-keyframes bgFadeOut{0%{opacity:1}100%{opacity:0}}@keyframes bgFadeOut{0%{opacity:1}100%{opacity:0}}@-webkit-keyframes contentZoomIn{0%{-webkit-transform:translateY(-30%);transform:translateY(-30%);opacity:0}100%{-webkit-transform:translateY(-50%);transform:translateY(-50%);opacity:1}}@keyframes contentZoomIn{0%{-webkit-transform:translateY(-30%);transform:translateY(-30%);opacity:0}100%{-webkit-transform:translateY(-50%);transform:translateY(-50%);opacity:1}}';
	document.querySelector("head").appendChild(_style);
}(window)